package com.lqq.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqq.eduservice.entity.EduComment;
import com.lqq.eduservice.mapper.EduCommentMapper;
import com.lqq.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-11
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    //分页查询课程评论
    @Override
    public Map<String, Object> getCourseComment(long current, long limit, String courseId) {
        Page<EduComment> page = new Page<>(current, limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId)
               .orderByDesc("gmt_create");
        baseMapper.selectPage(page, wrapper);

        List<EduComment> commentList = page.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("current", page.getCurrent());
        map.put("pages", page.getPages());
        map.put("size", page.getSize());
        map.put("total", page.getTotal());
        map.put("hasNext", page.hasNext());
        map.put("hasPrevious", page.hasPrevious());

        return map;
    }

    //分页查询课程评论（管理员）
    @Override
    public Map<String, Object> getAdminCourseComment(long current, long limit) {
        Page<EduComment> page = new Page<>(current, limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(page, wrapper);

        List<EduComment> commentList = page.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("total", page.getTotal());

        return map;
    }

}
